//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BlackIborQuantoCouponPricer.h"
using namespace Cephei::QL::Experimental::Coupons;
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVolTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletVolatilityStructure.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/BlackIborCouponPricer.h>
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
using namespace Cephei::QL::Cashflows;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::CBlackIborQuantoCouponPricer (Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ fxRateBlackVolatility, Cephei::QL::IQuote^ underlyingFxCorrelation, Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^ capletVolatility) : CBlackIborCouponPricer(CBlackIborQuantoCouponPricer::typeid)
{
    CBlackVolTermStructure^ _CfxRateBlackVolatility;
    CQuote^ _CunderlyingFxCorrelation;
    COptionletVolatilityStructure^ _CcapletVolatility;
    try
    {
#ifdef HANDLE
        _phBlackIborQuantoCouponPricer = NULL;
#endif
        _CfxRateBlackVolatility = safe_cast<CBlackVolTermStructure^> (fxRateBlackVolatility);
        _CfxRateBlackVolatility->Lock();
        Handle<QuantLib::BlackVolTermStructure>& _fxRateBlackVolatility = static_cast<Handle<QuantLib::BlackVolTermStructure>&> (_CfxRateBlackVolatility->GetHandle ()); 
        _CunderlyingFxCorrelation = safe_cast<CQuote^> (underlyingFxCorrelation);
        _CunderlyingFxCorrelation->Lock();
        Handle<QuantLib::Quote>& _underlyingFxCorrelation = static_cast<Handle<QuantLib::Quote>&> (_CunderlyingFxCorrelation->GetHandle ()); 
        _CcapletVolatility = safe_cast<COptionletVolatilityStructure^> (capletVolatility);
        _CcapletVolatility->Lock();
        Handle<QuantLib::OptionletVolatilityStructure>& _capletVolatility = static_cast<Handle<QuantLib::OptionletVolatilityStructure>&> (_CcapletVolatility->GetHandle ()); 
        _ppBlackIborQuantoCouponPricer = new boost::shared_ptr<QuantLib::BlackIborQuantoCouponPricer> (new QuantLib::BlackIborQuantoCouponPricer ( _fxRateBlackVolatility,  _underlyingFxCorrelation,  _capletVolatility ));
        SetBlackIborCouponPricer (boost::dynamic_pointer_cast<QuantLib::BlackIborCouponPricer> (*_ppBlackIborQuantoCouponPricer));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfxRateBlackVolatility != nullptr) _CfxRateBlackVolatility->Unlock();
        if (_CunderlyingFxCorrelation != nullptr) _CunderlyingFxCorrelation->Unlock();
        if (_CcapletVolatility != nullptr) _CcapletVolatility->Unlock();
    }
}
Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::CBlackIborQuantoCouponPricer (boost::shared_ptr<QuantLib::BlackIborQuantoCouponPricer>& childNative, Object^ owner) : CBlackIborCouponPricer(CBlackIborQuantoCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackIborQuantoCouponPricer = NULL;
#endif
	_ppBlackIborQuantoCouponPricer = &childNative;
    _ppBlackIborCouponPricer = new boost::shared_ptr<QuantLib::BlackIborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::BlackIborCouponPricer> (*_ppBlackIborQuantoCouponPricer));
}
Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::CBlackIborQuantoCouponPricer (QuantLib::BlackIborQuantoCouponPricer& childNative, Object^ owner) : CBlackIborCouponPricer(CBlackIborQuantoCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackIborQuantoCouponPricer = NULL;
#endif
	_ppBlackIborQuantoCouponPricer = new boost::shared_ptr<QuantLib::BlackIborQuantoCouponPricer> (&childNative);
    _ppBlackIborCouponPricer = new boost::shared_ptr<QuantLib::BlackIborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::BlackIborCouponPricer> (*_ppBlackIborQuantoCouponPricer));
    _BlackIborQuantoCouponPricerOwner = owner;
    _BlackIborCouponPricerOwner = owner;
}

Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::CBlackIborQuantoCouponPricer (CBlackIborQuantoCouponPricer^ copy) : CBlackIborCouponPricer(CBlackIborQuantoCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackIborQuantoCouponPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackIborQuantoCouponPricer = new boost::shared_ptr<QuantLib::BlackIborQuantoCouponPricer> (copy->GetShared());
        _ppBlackIborCouponPricer = new boost::shared_ptr<QuantLib::BlackIborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::BlackIborCouponPricer> (*_ppBlackIborQuantoCouponPricer));
    }
}
Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::CBlackIborQuantoCouponPricer (System::Type^ t) : CBlackIborCouponPricer(CBlackIborQuantoCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackIborQuantoCouponPricer = NULL;
#endif
	if (!t->IsSubclassOf(CBlackIborQuantoCouponPricer::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::CBlackIborQuantoCouponPricer (QuantLib::Handle<QuantLib::BlackIborQuantoCouponPricer>& childNative, Object^ owner)  : CBlackIborCouponPricer(CBlackIborQuantoCouponPricer::typeid)
{
	_phBlackIborQuantoCouponPricer = &childNative;
	_ppBlackIborQuantoCouponPricer = &static_cast<boost::shared_ptr<QuantLib::BlackIborQuantoCouponPricer>>(childNative.currentLink());
    _ppBlackIborCouponPricer = new boost::shared_ptr<QuantLib::BlackIborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::BlackIborCouponPricer> (*_ppBlackIborQuantoCouponPricer));
    _BlackIborQuantoCouponPricerOwner = owner;
}
Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::CBlackIborQuantoCouponPricer (QuantLib::Handle<QuantLib::BlackIborQuantoCouponPricer> childNative)  : CBlackIborCouponPricer(CBlackIborQuantoCouponPricer::typeid)
{
	_phBlackIborQuantoCouponPricer = &childNative;
	_ppBlackIborQuantoCouponPricer = &static_cast<boost::shared_ptr<QuantLib::BlackIborQuantoCouponPricer>>(childNative.currentLink());
    _ppBlackIborCouponPricer = new boost::shared_ptr<QuantLib::BlackIborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::BlackIborCouponPricer> (*_ppBlackIborQuantoCouponPricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::CBlackIborQuantoCouponPricer (QuantLib::BlackIborQuantoCouponPricer childNative)  : CBlackIborCouponPricer(CBlackIborQuantoCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackIborQuantoCouponPricer = NULL;
#endif
	_ppBlackIborQuantoCouponPricer = new boost::shared_ptr<QuantLib::BlackIborQuantoCouponPricer> (new QuantLib::BlackIborQuantoCouponPricer (childNative));
    _ppBlackIborCouponPricer = new boost::shared_ptr<QuantLib::BlackIborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::BlackIborCouponPricer> (*_ppBlackIborQuantoCouponPricer));
}
#endif

Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::~CBlackIborQuantoCouponPricer ()
{
    if (_ppBlackIborQuantoCouponPricer != NULL)
    {
	    delete _ppBlackIborQuantoCouponPricer;
        _ppBlackIborQuantoCouponPricer = NULL;
    }
}
Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::!CBlackIborQuantoCouponPricer ()
{
    if (_ppBlackIborQuantoCouponPricer != NULL)
    {
	    delete _ppBlackIborQuantoCouponPricer;
    }
}
QuantLib::BlackIborQuantoCouponPricer& Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::GetReference ()
{
    if (_ppBlackIborQuantoCouponPricer == NULL) throw gcnew NativeNullException ();
	return **_ppBlackIborQuantoCouponPricer;
}
boost::shared_ptr<QuantLib::BlackIborQuantoCouponPricer>& Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::GetShared ()
{
    if (_ppBlackIborQuantoCouponPricer == NULL) throw gcnew NativeNullException ();
	return *_ppBlackIborQuantoCouponPricer;
}
QuantLib::BlackIborQuantoCouponPricer* Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::GetPointer ()
{
    if (_ppBlackIborQuantoCouponPricer == NULL) throw gcnew NativeNullException ();
	return &**_ppBlackIborQuantoCouponPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackIborQuantoCouponPricer>& Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::GetHandle ()
{
	if (_phBlackIborQuantoCouponPricer == NULL)
	{
		_phBlackIborQuantoCouponPricer = new Handle<QuantLib::BlackIborQuantoCouponPricer> (*_ppBlackIborQuantoCouponPricer);
	}
	return *_phBlackIborQuantoCouponPricer;
}
#endif
bool Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer::HasNative () 
{
	return (_ppBlackIborQuantoCouponPricer != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Coupons::IBlackIborQuantoCouponPricer^ Cephei::QL::Experimental::Coupons::CBlackIborQuantoCouponPricer_Factory::Create (Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ fxRateBlackVolatility, Cephei::QL::IQuote^ underlyingFxCorrelation, Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^ capletVolatility)
{
    return gcnew CBlackIborQuantoCouponPricer ( fxRateBlackVolatility,  underlyingFxCorrelation,  capletVolatility);
}
